package com.whq.thrift.api.web.base.serviceconf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.thrift.api.common.spring.datasource.DataSourceContextHolder;
import com.whq.thrift.api.web.base.serviceconf.db.UrlMappingInfo;
import com.whq.thrift.api.web.base.serviceconf.db.UrlMappingInfoMapper;
import com.whq.thrift.api.web.base.serviceconf.service.UrlMappingInfoService;

@Service
public class UrlMappingInfoServiceImpl extends UrlMappingInfoService {
	@Autowired
    private UrlMappingInfoMapper urlMappingInfoMapper;
	
	public void addUrlMappingInfo(UrlMappingInfo urlMappingInfo) {
		if (isGray){
			DataSourceContextHolder.set("dataSourceBaseGrayServiceConfig");
		}else{
			DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		}
		
		if (urlMappingInfo == null) {
			return ;
		}
		Date now  = new Date();
		urlMappingInfo.setCreateTime(now);
		urlMappingInfo.setUpdateTime(now);
		
		this.urlMappingInfoMapper.addUrlMappingInfo(urlMappingInfo);
	}
	
	public void deleteUrlMappingInfo(String url) {
		if (isGray){
			DataSourceContextHolder.set("dataSourceBaseGrayServiceConfig");
		}else{
			DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		}
		
		this.urlMappingInfoMapper.deleteUrlMappingInfo(url);
	}


	public void updateUrlMappingInfo(UrlMappingInfo urlMappingInfo) {
		if (isGray){
			DataSourceContextHolder.set("dataSourceBaseGrayServiceConfig");
		}else{
			DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		}
		if (urlMappingInfo == null) {
			return ;
		}
		Date now  = new Date();
		urlMappingInfo.setUpdateTime(now);
		
		this.urlMappingInfoMapper.updateUrlMappingInfo(urlMappingInfo);
	}

	
	
	public List<UrlMappingInfo> findUrlMappingInfoByMap(String url, int beginIndex, int maxCount) {
		if (isGray){
			DataSourceContextHolder.set("dataSourceBaseGrayServiceConfig");
		}else{
			DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		}
		
//		if (whereParams == null) {
//			whereParams = new HashMap<String, Object>(0);
//		}
//		whereParams.put("beginIndex", beginIndex);
//		whereParams.put("maxCount", maxCount);
		return this.urlMappingInfoMapper.findUrlMappingInfoByMap(url, beginIndex, maxCount);
	}

	public int findUrlMappingInfoCount(String url) {
		if (isGray){
			DataSourceContextHolder.set("dataSourceBaseGrayServiceConfig");
		}else{
			DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		}
		
		return this.urlMappingInfoMapper.findUrlMappingInfoCount(url);
	}
	
	public UrlMappingInfo findUrlMappingInfo(String url) {
		if (isGray){
			DataSourceContextHolder.set("dataSourceBaseGrayServiceConfig");
		}else{
			DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		}
		
		return this.urlMappingInfoMapper.findUrlMappingInfo(url);
	}
	
	public List<UrlMappingInfo> findAllUrlMappingInfo() {
		if (isGray){
			DataSourceContextHolder.set("dataSourceBaseGrayServiceConfig");
		}else{
			DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		}
		
		return this.urlMappingInfoMapper.findAllUrlMappingInfo();
	}

}